using System;
using newtelligence.DasBlog.Web.Core;

namespace newtelligence.DasBlog.Web
{
    /// <summary>
    /// Responsible for creating edit controls
    /// </summary>
    public class EditControlProvider
    {
        /// <summary>
        /// Creates and returns instance of a <see cref="Core.EditControlAdapter"/> based on the site configuration.
        /// </summary>
        /// <returns>An instance of <see cref="Core.EditControlAdapter"/></returns>
        public EditControlAdapter CreateEditControl()
        {
            SiteConfig siteConfig = SiteConfig.GetSiteConfig();
            string configuredEditControl = siteConfig.EntryEditControl;
            EditControlAdapter editControl = null;

            if (configuredEditControl != null && configuredEditControl.Length > 0)
            {
                Type editControlType = Type.GetType(configuredEditControl, false, false);

                if (editControlType != null)
                {
                    //TODO: it might be better to cache the ConstructorInfo reference in a static field
                    editControl = Activator.CreateInstance(editControlType) as EditControlAdapter;
                }
            }

            // if the configured control cannot be loaded, default to the freetextbox
            if (editControl == null)
            {
                editControl = new FreeTextBoxAdapter();
            }

            return editControl;
        }
    }
}